﻿// author : 招

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.IO;
using System.Xml.Linq;

namespace DragDraw.Service
{
    /// <summary>
    /// 用于服务器的一些辅助方法
    /// </summary>
    public class ServerUtils
    {
        /// <summary>
        /// 获取服务器的本地路径
        /// </summary>
        /// <returns>服务器的本地路径，形如"E:\WebRoot\"</returns>
        public static string GetLocalPath()
        {
            return GetLocalPath("");
        }

        /// <summary>
        /// 获取服务器的本地路径，并与subpath合并
        /// </summary>
        /// <param name="subpath">服务所在路径下的文件名</param>
        /// <returns>subpath所在的本地路径，形如"E:\WebRoot\abc.txt"</returns>
        public static string GetLocalPath(string subpath)
        {
            string servPath = "";
            try
            {
                // use web.config
                servPath = System.Web.Configuration.
                    WebConfigurationManager.AppSettings["BasePath"];
                
            }
            catch
            {
                // for asp.net
                servPath = HttpContext.Current.Server.MapPath("");
            }

            return Path.Combine(servPath, subpath);
        }

        /// <summary>
        /// 获取服务器的Web虚拟路径
        /// </summary>
        /// <returns>返回服务的Web路径，形如"~/UserService.svc"</returns>
        public static string GetVirtualPath()
        {
            VirtualPathExtension extension = OperationContext.
                Current.Host.Extensions.Find<VirtualPathExtension>();
            return extension.VirtualPath;
        }
    }
}
